/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ems.dao.JoinDao;
import com.thinkgem.jeesite.modules.ems.entity.Join;

/**
 * 报名管理Service
 * @author zad
 * @version 2017-10-16
 */
@Service
@Transactional(readOnly = true)
public class JoinService extends CrudService<JoinDao, Join> {

	
	@Resource
    private JoinDao joinDao;
	
	public Join get(String id) {
		return super.get(id);
	}
	
	public List<Join> findList(Join join) {
		return super.findList(join);
	}
	
	public Page<Join> findPage(Page<Join> page, Join join) {
		return super.findPage(page, join);
	}
	
	@Transactional(readOnly = false)
	public void save(Join join) {
		super.save(join);
	}
	
	// 批量插入报名信息
	@Transactional(readOnly = false)
	public int saveBatch(List<Join> joinList)
	{
		return joinDao.insertBatch(joinList);
	}
	
	// 报名信息查询功能单独列出，因为删除的数据也需要查询出来 zad 2017-12-08
	public List<Join> findListForSearch(Join join)
	{
		return joinDao.findListForSearch(join);
	}
	
	// 将多个选手名称及对应id转换为list，用于后台批量插入
	public Join parseJionList(Join join)
	{
		if(null == join || StringUtils.isBlank(join.getJoiner()))
		{
			return null;
		}
		List<Map<String,String>> joinerList = new ArrayList<Map<String,String>>();
		String joiner = join.getJoiner();
		String []joinerArr = joiner.split(",");
		for(String j:joinerArr)
		{
			//joinerList.add(j);
			String id=IdGen.uuid();
			Map<String,String> map = new HashMap<String, String>();
			map.put("id", id);
			map.put("joiner", j);
			
			joinerList.add(map);
		}
		join.setJoinerList(joinerList);
		return join;
	}
	
	@Transactional(readOnly = false)
	public void delete(Join join) {
		super.delete(join);
	}
	
	@Transactional(readOnly = false)
	public int deleteBatch(List<Join>joins) {
	  if(null == joins || joins.size() < 1)
	  {
		 return 0;
	  }
		
	  int size = joins.size();
	  if(size < 1001)
  	  {
  		  return this.joinDao.deleteBatch(joins);
  	  }
  	  else
  	  {
  		  int rows = 0;
  		  int wholeCount = size/1000;
  		  int left = size%1000;
  		  int biginIndex = 0;
  		  for(int i = 0; i < wholeCount; i++)
  		  {
  			  List<Join> subList = joins.subList(biginIndex, biginIndex + 1000);
  			  biginIndex += 1000;
  			  rows += this.joinDao.deleteBatch(subList);
  		  }
  		  List<Join> subList = joins.subList(biginIndex, biginIndex + left);
  		  rows += joinDao.deleteBatch(subList);
  		  return rows;
  	  }
		
	}
	
}